package com.xiaoran.rabbitmq.demo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;

import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayedQueueCofig {
    //队列
    public static final String delayed_queueu_name="delayed.queue";
    //交换机
    public static final String delayed_exchange_name="delayed.exchange";
    //RoutingKey
    public static final String delayed_routuing_key="delayed.routingkey";

    //声明队列
    @Bean
    public Queue getQueue(){
        return new Queue(delayed_queueu_name);
    }
    //声明自定义（CustomExchange）交换机
    @Bean
    public CustomExchange customExchange(){
        Map<String, Object> arguments =new HashMap<>();
        //设置过期时间
        arguments.put("x-delayed-type","direct");
        return new CustomExchange(delayed_exchange_name,"x-delayed-message",true,false,arguments);
    }

    //绑定交换机与队列
    @Bean
    public Binding delayer_QUeueu_Exchange(@Qualifier("getQueue") Queue queue,
                                           @Qualifier("customExchange") CustomExchange customExchange){
        return BindingBuilder.bind(queue).to(customExchange).with(delayed_routuing_key).noargs();
    };
}
